package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.Cartype;
import com.tosit.bmx.service.admin.CarManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 车型管理类
 *
 * @author tsl
 * @create 2018-04-24 14:28
 **/
@Controller
@RequestMapping()
public class CarController {
    @Autowired
    CarManagerService carManagerService;

    /**
     * 添加一个车型。
     *
     * @param cartype
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/cartype", method = RequestMethod.POST)
    public void addCartype(Cartype cartype, @RequestParam("courseImg") MultipartFile file , HttpServletRequest request, HttpServletResponse response) {
        Integer result = null;
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/ico/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                cartype.setImgpath("/upload/ico/"+ newFileName);
                result= carManagerService.addCartype(cartype);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result= carManagerService.addCartype(cartype);
         if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个车型
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/cartype", method = RequestMethod.DELETE)
    public void removeCartype(Integer id, HttpServletResponse response) {
        Integer result = carManagerService.removeCartype(id);
         if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 修改一个车型
     *
     * @param cartype
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/cartype/fix", method = RequestMethod.POST)
    public void modifyCartype(Cartype cartype,@RequestParam("courseImg") MultipartFile file , HttpServletRequest request, HttpServletResponse response) {
        Integer result = null;
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/ico/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                cartype.setImgpath("/upload/ico/"+ newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result = carManagerService.modifyCartype(cartype);
         if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

}
